using System.Collections.Generic;
using BasicScript.Component;
using Game.Main.Model;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class ThermometerItem : BaseItem
	{
		private int MIN_TEMPATURE = 36;

		private int MAX_TEMPATURE = 42;

		private int temprature;

		private float timer;

		private Transform numTrans;

		private SpriteRenderer[] numRenders;

		private SpriteAssets spriteAssets;

		private List<ItemComponentTrigger> targetItems = new List<ItemComponentTrigger>();

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			spriteAssets = model.GetComponent<SpriteAssets>();
			numTrans = model.Find("Num");
			numTrans.SetLocalZ(-1E-05f);
			if (data.state != 0)
			{
				SetNumber(data.state, numTrans);
			}
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (targetItems.Count > 0)
			{
				timer += Time.deltaTime;
				if (timer > 0.2f)
				{
					timer = 0f;
					SetNumber(Mathf.Clamp(temprature + Random.Range(-1, 2), MIN_TEMPATURE, MAX_TEMPATURE), numTrans);
				}
			}
		}

		protected override void OnItemCompTriggerEnter(ColliderData collData)
		{
			base.OnItemCompTriggerEnter(collData);
			if (collData.item != null && collData.item.itemData.scriptType == 1 && (collData.trigger.type == ItemComponentTrigger.Type.Mouth || collData.trigger.type == ItemComponentTrigger.Type.Body || collData.trigger.type == ItemComponentTrigger.Type.Face || collData.trigger.type == ItemComponentTrigger.Type.Head || collData.trigger.type == ItemComponentTrigger.Type.LeftHand || collData.trigger.type == ItemComponentTrigger.Type.RightHand) && !targetItems.Contains(collData.trigger))
			{
				targetItems.Add(collData.trigger);
				if (targetItems.Count == 1)
				{
					ResetTemperature();
				}
			}
		}

		protected override void OnItemCompTriggerExit(ColliderData collData)
		{
			base.OnItemCompTriggerExit(collData);
			targetItems.Remove(collData.trigger);
		}

		public override void TouchUp(TouchEventData touchEventData)
		{
			base.TouchUp(touchEventData);
			targetItems.Clear();
		}

		private void ResetTemperature()
		{
			timer = 0f;
			temprature = Random.Range(MIN_TEMPATURE, MAX_TEMPATURE);
			SetNumber(temprature, numTrans);
		}

		public void SetNumber(int num, Transform numTrans)
		{
			temprature = num;
			string[] array = new string[2];
			if (num < 10)
			{
				array[0] = "0";
				array[1] = num.ToString();
			}
			else
			{
				array[0] = num.ToString().Substring(0, 1);
				array[1] = num.ToString().Substring(1, 1);
			}
			if (numRenders == null)
			{
				numRenders = new SpriteRenderer[2];
				numRenders[0] = numTrans.Find("0").GetComponent<SpriteRenderer>();
				numRenders[1] = numTrans.Find("1").GetComponent<SpriteRenderer>();
			}
			for (int i = 0; i < array.Length; i++)
			{
				string text = array[i];
				numRenders[i].sprite = spriteAssets.GetSprite(text);
			}
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = temprature;
		}
	}
}
